package cn.wolfcode.trip.app.interceptor;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器 当前没有用户登录时 其拦截作用 并跳转到登录界面
 */
public class LoginInterceptor extends HandlerInterceptorAdapter{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断当前session中是否存在用户  存在说明已经登录 就放行,否则拦截
        User user = UserContext.getCurrentUser();
        if (user == null) {
            //跳转到登录界面
            response.sendRedirect("/login.html");
            return false;
        }
        return true;
    }
}
